package meiRiYiTi;

import java.util.List;

/**
 * @Author: wzp
 * @CreateTime: 2024-09-03  21:47
 * @Description: 每日一题第2708 一个小组的最大实力值
 * @Version: 1.0
 */
public class day_2708 {
    public long maxStrength(int[] nums) {
        long result=1;
//      先处理特殊的情况，也就是只有一个数的情况
        if(nums.length==1){
            return nums[0];
        }
        int zero_num=0;
        int fu_min=Integer.MAX_VALUE;
        int start_index=-1;
        for (int i=0;i<nums.length;i++){
            if (nums[i]!=0){
                result=nums[i];
                start_index=i;
                if(nums[i]<0){
                    fu_min=Math.min(fu_min,Math.abs(nums[i]));
                }
                break;
            }else {
                zero_num++;
            }
        }
        if(start_index==-1){
            return 0;
        }
        for (int i=start_index+1;i<nums.length;i++){
            if(nums[i]==0){
                zero_num++;
                continue;
            }
            result*=nums[i];
            if(nums[i]<0){
                fu_min=Math.min(fu_min,Math.abs(nums[i]));
            }

        }
        if(result<0&&zero_num==nums.length-1){
            return 0;
        }
        if(result>0){
            return result;
        }else {
            return Math.abs(result)/fu_min;
        }

    }

    public int countWays(List<Integer> nums) {
//        将nums数组进行排序
        nums.sort(Integer::compareTo);
        int result=0;
        int len=nums.size();
        if(nums.get(0)>0){
            result++;
        }
        if(nums.get(len-1)<len){
            result++;
        }
        if(len==1){
            return result;
        }
        for (int i=1;i<=len-1;i++){
            if(nums.get(i-1)<i&&nums.get(i)>i){
                result++;
            }
        }
        return result;
    }


    public String clearDigits(String s) {
        char[] arr = s.toCharArray();
        int len=arr.length;
        int [] tag=new int[len];
        int delete_num=0;
        for (int i = 0; i < len; i++) {
            if(arr[i]>='0'&&arr[i]<='9'){
                tag[i]=1;
                delete_num++;
                for (int j = i-1; j>=0; j--){
                    if (tag[j]==0){
                        tag[j]=1;
                        delete_num++;
                        break;
                    }
                }
            }
        }
        char[] result=new char[len-delete_num];
        int index=0;
        for (int i = 0; i < len; i++) {
            if(tag[i]==0){
                result[index++]=arr[i];
            }
        }
        return new String(result);
    }
    public String clearDigits2(String s) {

//        采用栈的方式重做一遍
        StringBuilder result=new StringBuilder();
        for (int i = 0; i < s.length();i++){
            char ch=s.charAt(i);
            if(ch>='0'&&ch<='9'){
                result.deleteCharAt(result.length()-1);
            }else {
                result.append(ch);
            }
        }
        return result.toString();
    }

    public static void main(String[] args) {
//        [-1,-7,-5,7,7,0,9,0,-5,-6]
        int[] nums={-1,-7,-5,7,7,0,9,0,-5,-6};
        day_2708 day_2708=new day_2708();
        System.out.println(day_2708.maxStrength(nums));
    }
}
